using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;

namespace PluginHandler.UI
{
    internal partial class ConnectionDialog : UserControl, IConnectionDialog
    {
        private DbConnectionStringBuilder _connectionStringBuilder;

        public ConnectionDialog()
        {
            InitializeComponent();
        }

        #region IConnectionDialog Member

        public DbConnectionStringBuilder ConnectionStringDetails
        {
            get 
            {
                if (_connectionStringBuilder == null)
                    _connectionStringBuilder = new DbConnectionStringBuilder();

                _connectionStringBuilder["Data Source"] = txtServer.Text;
                _connectionStringBuilder["Initial Catalog"] = txtDatabase.Text;
                _connectionStringBuilder["Integrated Security"] = cmbAuthentication.SelectedIndex == 0 ? "SSPI" : "";
                _connectionStringBuilder["User Id"] = txtUsername.Text;
                _connectionStringBuilder["Password"] = txtPassword.Text;

                return _connectionStringBuilder;
            }
            set 
            {
                _connectionStringBuilder = value;

                txtServer.Text = _connectionStringBuilder["Data Source"].ToString();
                txtDatabase.Text = _connectionStringBuilder["Initial Catalog"].ToString();
                cmbAuthentication.SelectedIndex = _connectionStringBuilder.ContainsKey("Integrated Security") ? 0 : 1;
                txtUsername.Text = _connectionStringBuilder["User Id"].ToString();
                txtPassword.Text = _connectionStringBuilder["Password"].ToString();
            }
        }

        #endregion

        private void cmbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAuthentication.SelectedIndex == 0)
            {
                pnlSQLAuthentication.Enabled = false;
            }
            else
            {
                pnlSQLAuthentication.Enabled = true;
            }
        }
    }
}
